﻿#include "OiPCH.hpp"
#include "ui_OiSettingsDlg.h"

namespace Oi
{
    namespace PLScannerCore
    {
        class SettingsDlgImpl : public Impl<SettingsDlg>
        {
        public:
            void init(SettingsDlg::SettingMode mode);
            void setupParams();
            void saveParams();

            void setupLocal(const QVariantMap);
            QVariantMap local() const;

        public:
            Ui::SettingsDlg ui;
        };

        void SettingsDlgImpl::init(SettingsDlg::SettingMode mode)
        {
            OI_Q(SettingsDlg);
            ui.setupUi(q);

            setupParams();
        }

        void SettingsDlgImpl::setupParams()
        {
            OI_Q(SettingsDlg);
            ui._vel->setValue(PLScanner::instance()->acquireVel);
            ui._dir->setCurrentIndex(PLScanner::instance()->acquireDir ? 0 : 1);
            ui._captureTimeout->setCurrentIndex((0.1 * PLScanner::instance()->captureTimeout) - 1);
            ui._sideLen->setValue(PLScanner::instance()->sideLen);
            ui._dataInterval->setCurrentText(QString::number(PLScanner::instance()->pointMeasureInterval));
            ui._zeroMethod->setCurrentIndex(PLScanner::instance()->zeroMethod);
            ui._calibPos->setValue(PLScanner::instance()->calibPos);
            ui._autoCalib->setChecked(PLScanner::instance()->autoCalib);
            ui._calibInterval->setValue(PLScanner::instance()->calibInterval);
            ui._resultDir->setText(PLScanner::instance()->recordDir);
            ui._excelDir->setText(PLScanner::instance()->excelDir);
            ui._switchMode->setCurrentIndex(PLScanner::instance()->paramMode);
            ui._switchDelayTime->setValue(PLScanner::instance()->switchDelayStopTime);
            ui._switchStartDelayTime->setValue(PLScanner::instance()->switchDelayStartTime);
            ui._widthHide->setCurrentIndex(PLScanner::instance()->widthHide);
            ui._formalType->setCurrentIndex(PLScanner::instance()->formalType);
            ui._offset->setValue(PLScanner::instance()->measureOffsetY_S);
            ui._widthET->setValue(PLScanner::instance()->widthETDefault);
            ui._edgeThickness->setValue(PLScanner::instance()->edgeThickness);
            ui._compensation->setValue(PLScanner::instance()->compensation);
            ui._precision->setValue(PLScanner::instance()->precisionDefault);
            ui._widthWT->setValue(PLScanner::instance()->widthWTDefault);
            ui._thicknessET->setValue(PLScanner::instance()->thicknessETDefault);
            ui._thicknessWT->setValue(PLScanner::instance()->thicknessWTDefault);
            ui._thicknessValue->setChecked(PLScanner::instance()->profileDispalyOption & MT_MeasureThickness);
            ui._widthValue->setChecked(PLScanner::instance()->profileDispalyOption & MT_TotolWidth);
            ui._definitionValue->setChecked(PLScanner::instance()->profileDispalyOption & MT_DefinitionThickness);
            ui._acqPointMethod->setCurrentIndex(PLScanner::instance()->measureAcqPointMethod);
            ui._acqPointLen->setValue(PLScanner::instance()->measureAcqPointLen);
            ui._offsetX->setValue(PLScanner::instance()->measureOffsetX);

            ui._vel->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._dir->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._captureTimeout->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._sideLen->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._dataInterval->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._zeroMethod->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._calibPos->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._autoCalib->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._calibInterval->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._resultDirButton->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._excelDirButton->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._switchMode->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._switchDelayTime->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._switchStartDelayTime->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._widthHide->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._formalType->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._widthET->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._precision->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._compensation->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._edgeThickness->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._thicknessValue->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._widthWT->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._thicknessET->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._thicknessWT->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._acqPointMethod->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._acqPointLen->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);

            ui._offset->setEnabled(PLScanner::instance()->super);
            ui._offsetX->setEnabled(PLScanner::instance()->super);

            ui._offsetLabel->setVisible(PLScanner::instance()->super);
            ui._offset->setVisible(PLScanner::instance()->super);

            ui._offsetXLabel->setVisible(PLScanner::instance()->super);
            ui._offsetX->setVisible(PLScanner::instance()->super);
//             ui._acqPointMethodLabel->setVisible(false);
//             ui._acqPointMethod->setVisible(false);
<<<<<<< Updated upstream

=======
            ui._measureGroup->setChecked(false);
            ui._measureET_1->setValue(Core::OiSettings()->value("Measure/measureEt1", 0.05).toDouble());
            ui._measureWT_1->setValue(Core::OiSettings()->value("Measure/measureWt1", 0.05).toDouble());
>>>>>>> Stashed changes
            q->onSwitchModeChanged();

            q->connect(ui._switchMode, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), q, &SettingsDlg::onSwitchModeChanged);
            q->connect(ui._resultDirButton, &QPushButton::clicked, q, &SettingsDlg::onDirChoose);
            q->connect(ui._excelDirButton, &QPushButton::clicked, q, &SettingsDlg::onDirChoose);
        }

        void SettingsDlgImpl::saveParams()
        {
            PLScanner::instance()->acquireVel = ui._vel->value();
            PLScanner::instance()->acquireDir = ui._dir->currentIndex() == 0;
            PLScanner::instance()->captureTimeout = (ui._captureTimeout->currentIndex() + 1) * 10;
            PLScanner::instance()->sideLen = ui._sideLen->value();
            PLScanner::instance()->pointMeasureInterval = ui._dataInterval->currentText().toDouble();
            PLScanner::instance()->zeroMethod = ui._zeroMethod->currentIndex();
            PLScanner::instance()->calibPos = ui._calibPos->value();
            PLScanner::instance()->autoCalib = ui._autoCalib->isChecked();
            PLScanner::instance()->calibInterval = ui._calibInterval->value();
            PLScanner::instance()->recordDir = ui._resultDir->text();
            PLScanner::instance()->excelDir = ui._excelDir->text();
            PLScanner::instance()->paramMode = ui._switchMode->currentIndex();
            PLScanner::instance()->switchDelayStopTime = ui._switchDelayTime->value();
            PLScanner::instance()->switchDelayStartTime = ui._switchStartDelayTime->value();
            PLScanner::instance()->widthHide = ui._widthHide->currentIndex();
            PLScanner::instance()->formalType = ui._formalType->currentIndex();
            PLScanner::instance()->measureOffsetY_S = ui._offset->value();
            PLScanner::instance()->measureOffsetX = ui._offsetX->value();
            PLScanner::instance()->widthWTDefault = ui._widthWT->value();
            PLScanner::instance()->widthETDefault = ui._widthET->value();
            PLScanner::instance()->edgeThickness = ui._edgeThickness->value();
            PLScanner::instance()->compensation = ui._compensation->value();
            PLScanner::instance()->precisionDefault = ui._precision->value();
            PLScanner::instance()->thicknessWTDefault = ui._thicknessWT->value();
            PLScanner::instance()->thicknessETDefault = ui._thicknessET->value();
            PLScanner::instance()->measureAcqPointMethod = ui._acqPointMethod->currentIndex();
            PLScanner::instance()->measureAcqPointLen = ui._acqPointLen->value();

            int displayOption = 0x0000;
            if (ui._thicknessValue->isChecked())
            {
                displayOption |= MT_MeasureThickness;
            }
            if (ui._widthValue->isChecked())
            {
                displayOption |= MT_TotolWidth;
            }
            if (ui._definitionValue->isChecked())
            {
                displayOption |= MT_DefinitionThickness;
            }

            PLScanner::instance()->profileDispalyOption = displayOption;
<<<<<<< Updated upstream
=======
            if (ui._measureGroup->isChecked())
            {
                Core::OiSettings()->setValue("Measure/measureEt1", ui._measureET_1->value());
                Core::OiSettings()->setValue("Measure/measureWt1", ui._measureWT_1->value());


                std::vector<double> valET;
                std::vector<double> valWT;
                valET.push_back(ui._measureET_1->value());
                valWT.push_back(ui._measureWT_1->value());
                QDir dir(PLScanner::instance()->productDir);
                for (auto info : dir.entryInfoList(QStringList() << "*.plsp", QDir::Files))
                {
                    ProductParams params;
                   
                    ProductParams::loadFromLocal(params, info.fileName().remove(".plsp"));
                    int i = 0;
                    for (int index = 100; index < 110 ; index++)
                    {
                        if (params.insParams.contains(index))
                        {
                            QVariantMap pamras = params.insParams[index].value<QVariantMap>();
                            QVariantMap map = pamras["y"].value<QVariantMap>();
                            if (i > valET.size() - 1)
                            {
                                i = 0;
                            }
                            map["WarningTol"] = valET[i];
                            map["ErrorTol"] = valWT[i];
                            
                            pamras["y"] = map;
                            params.insParams[index] = pamras;
                            i++;
                        }
                        
                    }
                    ProductParams::saveToLocal(params);
                }
            }
           
>>>>>>> Stashed changes
        }

        void SettingsDlgImpl::setupLocal(QVariantMap cm)
        {
        
        }

        QVariantMap SettingsDlgImpl::local() const
        {
            return QVariantMap();
        }

        SettingsDlg::SettingsDlg(QWidget* parent , SettingsDlg::SettingMode mode)
            :Ctrl::Dialog(parent)
        {
            OI_I(SettingsDlg)->init(mode);
        }

        void SettingsDlg::saveParams()
        {
            OI_F(SettingsDlg);

            d->saveParams();
        }

        void SettingsDlg::setLocalConfig(const QVariantMap cm)
        {
            OI_IMPL()->setupLocal(cm);
        }

        QVariantMap SettingsDlg::localConfig() const
        {
            return OI_IMPL()->local();
        }

        void SettingsDlg::accept()
        {
            __super::accept();
        }

        void SettingsDlg::onDirChoose()
        {
            OI_F(SettingsDlg);

            auto obj = sender();
            QString dir;
            if (obj == d->ui._resultDirButton)
            {
                dir = d->ui._resultDir->text();
            }
            else if (obj == d->ui._excelDirButton)
            {
                dir = d->ui._excelDir->text();
            }

            QString fileName = QFileDialog::getExistingDirectory(this, tr("选择文件夹") , dir);

            if (!fileName.isEmpty())
            {
                if (obj == d->ui._resultDirButton)
                {
                     d->ui._resultDir->setText(fileName);
                }
                else if (obj == d->ui._excelDirButton)
                {
                    d->ui._excelDir->setText(fileName);
                }
            }
        }

        void SettingsDlg::onSwitchModeChanged()
        {
            OI_F(SettingsDlg);
            bool mode2 = d->ui._switchMode->currentIndex() == 2;
            d->ui._switchDelayTime->setVisible(mode2);
            d->ui._switchDelayTimeLabel->setVisible(mode2);
            d->ui._switchStartDelayTime->setVisible(mode2);
            d->ui._switchStartDelayTimeLabel->setVisible(mode2);
        }

        QVariantMap SettingsDlg::config()
        {
            QVariantMap  cm;


            return cm;
        }

        void SettingsDlg::setConfig(const QVariantMap cm)
        {

        }
    }
}